//
//  PhotosView.m
//  SinaWebo
//
//  Created by 亦心 on 15/4/3.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "PhotosView.h"
#import "PhotoView.h"
#import "Photo.h"
#import "FWPhotoBrowserView.h"
#import "FWGroupModel.h"
#import "PicInfo.h"
#import "PicMetadata.h"
#import <SDWebImageManager.h>

#define kFirstW (kScreenW - 2 * 10)
#define kFirstH kFirstW*0.625

#define kSecondW (kFirstW * 0.5-4)
#define kSecondH kSecondW

@interface PhotosView ()
@end

@implementation PhotosView
- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    self=[super initWithCoder:coder];
    if (self) {
        [self initView];
    }
    return self;
}

-(void)initView{
    //只显示9张图片
    for (int index=0; index<9; index++) {
        PhotoView *photoView=[[PhotoView alloc]init];
        photoView.userInteractionEnabled=YES;
        photoView.tag=index;
        [photoView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTap:)]];
        [self addSubview:photoView];
    }
}


-(void)photoTap:(UITapGestureRecognizer *)recognizer{
    PhotoView *phtoView = (PhotoView *)recognizer.view;
    FWPhotoBrowserView *photosView = [[FWPhotoBrowserView alloc] init];
    photosView.photos = [self browerPhotos];
    [photosView show:phtoView];
}

- (NSArray *)browerPhotos{
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger index = 0; index < self.photos.count; index++) {
        PicInfo *photo = _photos[index];
        FWGroupModel *model = [[FWGroupModel alloc] init];
        model.url = [photo.bmiddle.url stringByReplacingOccurrencesOfString:@"or360" withString:@"bmiddle"];
        model.progress = photo.progress;
        model.fromView = self.subviews[index];
        [array addObject:model];
    }
    return [array copy];
}

-(void)setPhotos:(NSArray *)photos{
    _photos=photos;
    for (int index=0; index<9; index++) {
        PhotoView *photoView=self.subviews[index];
        if(index<photos.count){
            photoView.hidden=NO;
            PicInfo *photo = photos.firstObject;
            //设置控件的frame
            //计算最多有几列
            int maxCount=(photos.count<=4)?2:3;
            int col=index%maxCount;
            int row=index/maxCount;
            CGFloat photoX=col*(kPhotoW+kMargin);
            CGFloat photoY=row*(kPhotoH+kMargin);
            switch (photos.count) {
                case 1:{
                    photoView.frame=CGRectMake(photoX, photoY, photo.scalWith, photo.scalHeight);
                    break;
                }
                
                case 2:{
                    photoX = col * (kSecondW+kMargin);
                    photoView.frame=CGRectMake(photoX, photoY, kSecondW, kSecondH);
                    break;
                }
                
                case 3: case 4:{
                    photoX = col * (kSecondW+kMargin);
                    CGFloat photoY=row*(kSecondH+kMargin);
                    photoView.frame=CGRectMake(photoX, photoY, kSecondW, kSecondH);
                    break;
                }
                    
                default:
                    photoView.frame=CGRectMake(photoX, photoY, kPhotoW, kPhotoH);
                    break;
            }
            
            photoView.picInfo = photos[index];
        }else{
            photoView.hidden=YES;
        }
        
    }
}

+ (CGSize)photosViewSizeWithPhotosCount:(int)count{
    // 一行最多有3列
    int maxColumns = (count <= 4) ? 2 : 3;
    
    //  总行数
    int rows = (count + maxColumns - 1) / maxColumns;
    // 高度
    CGFloat photosH = rows * kPhotoH + (rows - 1) * kMargin;
    
    // 总列数
    int cols = (count >= maxColumns) ? maxColumns : count;
    // 宽度
    CGFloat photosW = cols * kPhotoW + (cols - 1) * kMargin;

    switch (count) {
        case 1:{
            return CGSizeMake(kFirstW, kFirstH);
            break;
        }
        
        case 2:{
            return CGSizeMake(kScreenW - 20, kSecondH);
            break;
        }
            
        case 3: case 4:{
            return CGSizeMake(kScreenW - 20, kSecondH*2+4);
        }
            
            
        default:
            return CGSizeMake(photosW, photosH);
        break;
            
    }
    
    /**
     一共60条数据 == count
     一页10条 == size
     总页数 == pages
     pages = (count + size - 1)/size;
     */
}

+ (CGSize)photosViewSizeWithDataArrray:(NSArray *)imageArray{
    NSInteger count = imageArray.count;
    // 一行最多有3列
    NSInteger maxColumns = (count <= 4) ? 2 : 3;
    
    //  总行数
    NSInteger rows = (count + maxColumns - 1) / maxColumns;
    // 高度
    CGFloat photosH = rows * kPhotoH + (rows - 1) * kMargin;
    
    // 总列数
    NSInteger cols = (count >= maxColumns) ? maxColumns : count;
    // 宽度
    CGFloat photosW = cols * kPhotoW + (cols - 1) * kMargin;
    
    switch (count) {
        case 1:{
            CGFloat imgW = 0.0;
            CGFloat imgH = 0.0;
            PicInfo *picInfo = imageArray.firstObject;
            PicMetadata *bmiddle = picInfo.bmiddle;
            MyLog(@"%f, %f", bmiddle.width, bmiddle.height);
            if (picInfo) {
                CGFloat scale = bmiddle.height / bmiddle.width;
                CGFloat rscale = bmiddle.width / bmiddle.height;
              if (scale < 0.7) {
                  imgW = kFirstW;
                  imgH = imgW * scale;
                  picInfo.scalWith = imgW;
                  picInfo.scalHeight = imgH;
                  return CGSizeMake(imgW, imgH);
              }else if (scale >= 0.7 && scale <= 1.0){
                  imgW = kFirstW * 0.6;
                  imgH = imgW * scale;
                  picInfo.scalWith = imgW;
                  picInfo.scalHeight = imgH;
                  return CGSizeMake(imgW, imgH);
              } else if (scale > 1.0 && scale < 1.6) {
                  imgH = kFirstH;
                  imgW = rscale * imgH;
                  picInfo.scalWith = imgW;
                  picInfo.scalHeight = imgH;
                  return CGSizeMake(imgW, imgH);
                  
              }else{
                  imgW = kFirstW * 0.5;
                  imgH = imgW * 1.3;
                  picInfo.badgeType = PictureBadgeTypeLong;
                  picInfo.scalWith = imgW;
                  picInfo.scalHeight = imgH;
                  return CGSizeMake(imgW, imgH);
              }
               
            }else{
                picInfo.scalWith = imgW;
                picInfo.scalHeight = imgH;
                return CGSizeMake(imgW, imgH);
            }
           
            break;
        }
            
        case 2:{
            return CGSizeMake(kScreenW - 20, kSecondH);
            break;
        }
            
        case 3: case 4:{
            return CGSizeMake(kScreenW - 20, kSecondH*2+4);
        }
            
        default:
            return CGSizeMake(photosW, photosH);
            break;
            
    }
}

@end
